package test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

public class demo6 {
    //AtomicInteger 增加同一变量
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch = new CountDownLatch(2);
        AtomicInteger atomicInteger = new AtomicInteger(0);
        Thread t1 = new Thread(()-> {
            for(int i = 0; i < 100; i++){
                atomicInteger.getAndIncrement();
            }
            latch.countDown();
        });
        Thread t2 = new Thread(()-> {
            for(int i = 0; i < 100; i++){
                atomicInteger.getAndIncrement();
            }
            latch.countDown();
        });
        t1.start();
        t2.start();
        latch.await();
        System.out.println(atomicInteger);
    }
}
